From 6ec4a6e26fab5353a587c07a7b79a5ffb4be56b7 Mon Sep 17 00:00:00 2001
From: Vladislav Tarakanov <vladislav.tarakanov@bk.ru>
Date: Thu, 21 Mar 2024 03:01:48 +0400
Subject: [PATCH 4/6] Fix "Automatic" orientation

This reverts commit 12d2dd9f7e6e866f87557ce9d0644eec0f4cd447.

Change-Id: Ibd8bac03d068a7660e305b4c999752fdb0d35dba
---
 vcl/inc/printdlg.hxx           |  4 ++++
 vcl/source/window/printdlg.cxx | 30 ++++++++++++++++++++++++++++--
 2 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/vcl/inc/printdlg.hxx b/vcl/inc/printdlg.hxx
index 42210729cb9b..10d135117420 100644
--- a/vcl/inc/printdlg.hxx
+++ b/vcl/inc/printdlg.hxx
@@ -176,6 +176,8 @@ namespace vcl
         std::unique_ptr<weld::Widget>           mxCustom;
 
         // scale mode
+        std::unique_ptr<weld::Label>    mxScaleTxt;
+        std::unique_ptr<weld::Label>    mxScaleModeTxt;
         std::unique_ptr<weld::ComboBox> mxLbScaleMode;
         std::unique_ptr<weld::Widget> mxBxScaleAll;
         std::unique_ptr<weld::MetricSpinButton> mxEdScaleAll;
@@ -220,6 +222,8 @@ namespace vcl
 
         Paper                                   mePaper;
 
+        bool                                    mbIsCalcPrintDialog;
+
         Idle maUpdatePreviewIdle;
         DECL_LINK(updatePreviewIdle, Timer*, void);
         Idle maUpdatePreviewNoCacheIdle;
diff --git a/vcl/source/window/printdlg.cxx b/vcl/source/window/printdlg.cxx
index d30895376f6c..8dec96bc585f 100644
--- a/vcl/source/window/printdlg.cxx
+++ b/vcl/source/window/printdlg.cxx
@@ -594,6 +594,8 @@ PrintDialog::PrintDialog(weld::Window* i_pWindow, std::shared_ptr<PrinterControl
     , mxLayoutExpander(m_xBuilder->weld_expander("exLayoutExpander"))
     , mxCustom(m_xBuilder->weld_widget("customcontents"))
 
+    , mxScaleTxt(m_xBuilder->weld_label("label3d"))
+    , mxScaleModeTxt(m_xBuilder->weld_label("labelScalingMode"))
     , mxLbScaleMode(m_xBuilder->weld_combo_box("comboLB_SCALEMODE"))
     , mxBxScaleAll(m_xBuilder->weld_widget("boxSCALEALL"))
     , mxEdScaleAll(m_xBuilder->weld_metric_spin_button("spinED_SCALEALL", FieldUnit::PERCENT))
@@ -614,6 +616,7 @@ PrintDialog::PrintDialog(weld::Window* i_pWindow, std::shared_ptr<PrinterControl
     , maScaleTimer ("vcl wait_Impl")
     , mbCollateAlwaysOff(false)
     , mbShowLayoutFrame( true )
+    , mbIsCalcPrintDialog (false)
     , maUpdatePreviewIdle("Print Dialog Update Preview Idle")
     , maUpdatePreviewNoCacheIdle("Print Dialog Update Preview (no cache) Idle")
 {
@@ -1096,6 +1099,11 @@ void PrintDialog::updatePageSize(int nOrientation)
 
     PaperInfo aInfo = aPrt->GetPaperInfo(mxPaperSizeBox->get_active());
     Size aSize(aInfo.getWidth(), aInfo.getHeight());
+
+    // If paper size not defined by printer, then we need restore it to user defined size
+    if(aSize.IsEmpty())
+        aSize = getJobPageSize();
+
     if (aSize.IsEmpty())
         aSize = aPrt->GetSizeOfPaper();
 
@@ -1107,6 +1115,16 @@ void PrintDialog::updatePageSize(int nOrientation)
             aSize = Size(aSize.Height(), aSize.Width());
         }
     }
+    else
+    {
+        // If current orientation is different from user defined, then we need to swap width and height
+        Size originalSize = getJobPageSize();
+        if ( (aSize.Width() > aSize.Height() && originalSize.Width() < originalSize.Height())
+            || (aSize.Width() < aSize.Height() && originalSize.Width() > originalSize.Height()) )
+        {
+            aSize = Size(aSize.Height(), aSize.Width());
+        }
+    }
 
     aPrt->SetPrintPageSize(aSize);
     aPrt->SetUsePrintDialogSetting(true);
@@ -1175,7 +1193,8 @@ void PrintDialog::setPaperOrientation( Orientation eOrientation, bool fromUser )
 {
     VclPtr<Printer> aPrt( maPController->getPrinter() );
     aPrt->SetOrientation( eOrientation );
-    maPController->setOrientationFromUser( eOrientation, fromUser );
+    // Calc printing dialog should always be "from user". In other case "Automatic" orientation switches landscape to portrait
+    maPController->setOrientationFromUser( eOrientation, fromUser || mbIsCalcPrintDialog);
 }
 
 void PrintDialog::checkControlDependencies()
@@ -2471,6 +2490,7 @@ void PrintDialog::initScaleFromSheetStyle()
     css::awt::Size scaleWH = {};
     if((maPController->getFilteredScale(GENERAL_SCALING) >>= scaleAll) && scaleAll != 0)
     {
+        mbIsCalcPrintDialog = true;
         mxLbScaleMode->set_active(0);
         // controls for Box "Reduce/enlarge"
         mxBxScaleAll->set_visible(true);
@@ -2483,6 +2503,7 @@ void PrintDialog::initScaleFromSheetStyle()
     }
     else if ((maPController->getFilteredScale(WH_SCALING) >>= scaleWH) && (scaleWH.Height != 0 || scaleWH.Width != 0))
     {
+        mbIsCalcPrintDialog = true;
         mxLbScaleMode->set_active(1);
         // controls for Box "Reduce/enlarge"
         mxBxScaleAll->set_visible(false);
@@ -2503,6 +2524,7 @@ void PrintDialog::initScaleFromSheetStyle()
     }
     else if ((maPController->getFilteredScale(PAGE_SCALING) >>= scalePage) && scalePage != 0)
     {
+        mbIsCalcPrintDialog = true;
         mxLbScaleMode->set_active(2);
         // controls for Box "Reduce/enlarge"
         mxBxScaleAll->set_visible(false);
@@ -2515,8 +2537,12 @@ void PrintDialog::initScaleFromSheetStyle()
     }
     else
     {
+        // It`s not a calc
+        mxScaleTxt->set_visible(false);
+        mxScaleModeTxt->set_visible(false);
+        mxLbScaleMode->set_visible(false);
         // controls for Box "Reduce/enlarge"
-        mxBxScaleAll->set_visible(true);
+        mxBxScaleAll->set_visible(false);
         // controls for Grid "Scale to width/height"
         mxGrHeightWidth->set_visible(false);
         // controls for Box "Scale to pages"
-- 
2.40.1

